<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/admin' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>分类管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区 -->
    <el-card>
      <div class="text item">
        <!-- 搜索框区域 -->
        <el-row :gutter="20">
          <el-col :span="7">
            <el-input
              placeholder="请输入内容"
              clearable
              v-model="queryInfo.query"
              @clear="getTypeList"
            >
              <el-button
                slot="append"
                @click="getTypeList"
                icon="el-icon-search"
              ></el-button>
            </el-input>
          </el-col>
          <el-col :span="4">
            <el-button type="primary" @click="addDialogVisible = true"
              >添加分类</el-button
            >
          </el-col>
        </el-row>
        <!-- 用户列表区 -->
        <el-table :data="TypeList" border stripe>
          <el-table-column type="index"></el-table-column>
          <el-table-column label="分类" prop="name"></el-table-column>
          <el-table-column label="操作" width="180px">
            <template v-slot="scope">
              <!-- 修改 -->
              <el-button
                style="margin-left: 10px"
                type="primary"
                icon="el-icon-edit"
                size="mini"
                @click="showEditDialog(scope.row)"
              ></el-button>
              <!-- 删除 -->
              <el-popconfirm
                title="确认删除用户？"
                style="margin-left: 30px"
                @confirm="deleteType(scope.row)"
              >
                <el-button
                  slot="reference"
                  type="danger"
                  icon="el-icon-delete"
                  size="mini"
                ></el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页区域 -->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.pageNum"
          :page-sizes="[1, 2, 5, 10]"
          :page-size="queryInfo.pageSize"
          layout="total,sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </el-card>
    <!-- 修改用户的对话框 -->
    <el-dialog
      title="修改分类"
      :visible.sync="editDialogVisible"
      width="50%"
      @close="editDialogClose"
    >
      <!-- 主体区域 -->
      <el-form :model="editForm" ref="editFormRef" label-width="70px">
        <el-form-item label="分类">
          <el-input v-model="editForm.name"></el-input>
        </el-form-item>
      </el-form>
      <!-- 页脚区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editType">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 添加分类 -->
    <el-dialog
      title="添加分类"
      :visible.sync="addDialogVisible"
      width="50%"
      @close="addDialogClose"
    >
      <!-- 主体区域 -->
      <el-form :model="addForm" ref="addFormRef" label-width="70px">
        <el-form-item label="分类">
          <el-input v-model="addForm.name"></el-input>
        </el-form-item>
      </el-form>
      <!-- 页脚区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addType">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { deleteType, modifyType, modifyUser, addType } from '../../api/admin';
import { getTypeById, pagingType } from '../../api/topic';
export default {
  data() {
    return {
      queryInfo: {
        query: '',
        pageNum: 1,
        pageSize: 5,
      },
      TypeList: [],
      total: 0,
      editForm: {
        name: '',
        id: '',
      },
      addForm: {
        name: '',
      },
      editDialogVisible: false,
      addDialogVisible: false,
    };
  },
  methods: {
    async getTypeList() {
      const res = await pagingType({
        name: this.queryInfo.query,
        offset: this.queryInfo.pageNum,
        limit: this.queryInfo.pageSize,
      });
      this.TypeList = res.data.rows;
      this.total = res.data.count;
    },
    async userStateChanged(row) {
      const res = await modifyUser(row);
      if (res.code == 200) {
        this.$message({
          type: 'success',
          message: '修改用户状态成功',
        });
      }
    },
    handleSizeChange(size) {
      this.queryInfo.pageSize = size;
      this.getTypeList();
    },
    handleCurrentChange(page) {
      this.queryInfo.pageNum = page;
      this.getTypeList();
    },
    editDialogClose() {
      this.$refs.editFormRef.resetFields();
    },
    addDialogClose() {
      this.addForm.name = '';
    },
    async editType() {
      const res = await modifyType(this.editForm);
      if (res.code == 200) {
        this.$message({
          type: 'success',
          message: '修改分类成功',
        });
        this.getTypeList();
        this.editDialogVisible = false;
      }
    },
    async showEditDialog(row) {
      const { data: res } = await getTypeById(row.id);
      this.editForm.name = res.name;
      this.editForm.id = res.id;
      this.editDialogVisible = true;
    },
    async deleteType(row) {
      const res = await deleteType(row.id);
      if (res.code == 200) {
        this.getTypeList();
        this.$message({
          type: 'success',
          message: '删除分类成功',
        });
      }
    },
    async addType() {
      if (this.addForm.name != '' && this.addForm.name != null) {
        const res = await addType(this.addForm);
        if (res.code == 200) {
          this.getTypeList();
          this.$message({
            type: 'success',
            message: '添加分类成功',
          });
        }
      }
    },
  },
  created() {
    this.getTypeList();
  },
};
</script>
